﻿using System.Linq;
using System.Web.Mvc;
using Pyramid.Bll;
using Pyramid.Models;
using System.Data.Entity;

namespace Pyramid.Controllers
{
	public class AccountController : BaseController
	{
		private readonly AccountHelper _helper = new AccountHelper();

		/// <summary>
		/// 我的个人资料
		/// </summary>
		public ActionResult MyInfo()
		{
			switch (User.UserType)
			{
				case UserTypes.AdminCenter:
				{
					var model = Db.AdminCenters
					              .Include(x => x.MarketZone)
					              .FirstOrDefault(x => x.UserId == User.UserId);
					return model == null ? Error("个人资料已丢失，请联系管理员！") : View("AdminCenterInfo", model);
				}
				case UserTypes.Customer:
				{
					var model = Db.Customers.FirstOrDefault(x => x.UserId == User.UserId);
					return model == null ? Error("个人资料已丢失，请联系管理员！") : View("CustomerInfo", model);
				}
				case UserTypes.MemberCenter:
				{
					var model = Db.MemberCenters.FirstOrDefault(x => x.UserId == User.UserId);
					return model == null ? Error("个人资料已丢失，请联系管理员！") : View("MemberCenterInfo", model);
				}
				case UserTypes.Staff:
				{
					var model = Db.Staves
					              .Include(x => x.Department)
					              .Include(x => x.Position)
					              .FirstOrDefault(x => x.UserId == User.UserId);
					return model == null ? Error("个人资料已丢失，请联系管理员！") : View("StaffInfo", model);
				}
				case UserTypes.Developer:
				{
					var model = Db.Accounts.FirstOrDefault(x => x.UserId == User.UserId);
					return model == null ? Error("个人资料已丢失，请联系管理员！") : View("DeveloperInfo", model);
				}
			}

			return View("_Error", new ErrorViewModel {Message = "未知用户，请联系管理员！"});
		}

		/// <summary>
		/// 我的充值记录
		/// </summary>
		public ActionResult MyCharge(PagerQueryModel model)
		{
			if (User.UserType != UserTypes.Customer) return NoAuthorization("此功能仅对直接客户开放！");
			var query = PaymentHelper.NewInstance.MyPayment(User.ExtInfoId);

			var currentPage = (model.PageNum ?? 1) - 1;
			var pageSize = model.NumPerPage ?? 20;

			var vm = new PagerModel
			         {
				         PageSize = pageSize,
				         CurrentPageIndex = currentPage,
				         TotalCount = query.Count()
			         };
			ViewBag.Bag = vm;
			return View(query.OrderByDescending(x => x.Id).Skip(currentPage*pageSize).Take(pageSize).ToList());
		}

		/// <summary>
		/// 用户列表
		/// </summary>
		public ActionResult List(AccountQueryModel model)
		{
			var accounts = _helper.Query(model);
			var currentPage = (model.PageNum ?? 1) - 1;
			var pageSize = model.NumPerPage ?? 20;
			ViewBag.PagerModel = new PagerModel
			                     {
				                     CurrentPageIndex = currentPage,
				                     PageSize = pageSize,
				                     TotalCount = accounts.Count()
			                     };
			ViewBag.QueryModel = model;
			return View(accounts.OrderBy(x => x.Id).Skip(currentPage*pageSize).Take(pageSize).ToList());
		}
	}
}